package com.chat.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.GroupInfoServiceInterface;
import com.chat.api.dto.AuthPullBlackDTO;
import com.chat.api.entity.ChatBlack;
import com.chat.api.vo.AuthBlackListVO;
import com.chat.biz.mapper.BlackMapper;
import com.chat.common.exception.CommonException;
import com.chat.common.result.ResultEnum;
import com.chat.common.threadLocal.UserContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/*
*功能：
 作者：chenhao
*日期： 2025/6/7 下午5:04
*/
@Service
public class BlackService extends ServiceImpl<BlackMapper, ChatBlack>{

    @DubboReference
    private GroupInfoServiceInterface groupInfoService;

    public void pullblack(AuthPullBlackDTO dto) {
        String userId = UserContext.getUserId().getUserId();
        String blackedUserId = dto.getBlackedUserId();

        // 查询是否已拉黑该用户
        ChatBlack exist = baseMapper.selectOne(new LambdaQueryWrapper<ChatBlack>()
                .eq(ChatBlack::getUserId, userId)
                .eq(ChatBlack::getBlackedUserId, blackedUserId));

        if (exist != null) {
            // 已存在拉黑记录，可以抛异常或直接返回
            throw new CommonException(ResultEnum.USER_IS_BLACKED.getMsg());
            // 或者 return;
        }

        // 插入拉黑记录
        ChatBlack chatBlackList = new ChatBlack();
        chatBlackList.setUserId(userId);
        chatBlackList.setBlackedUserId(blackedUserId);
        baseMapper.insert(chatBlackList);
    }


    public Page<AuthBlackListVO> findpage(Page page) {
        List<AuthBlackListVO> authBlackListVO = baseMapper.selectBlackListorPageByUserId(page,UserContext.getUserId().getUserId());
        page.setRecords(authBlackListVO);
        return page;
    }

    public void pullout(AuthPullBlackDTO dto) {
        String userId = UserContext.getUserId().getUserId();
        String blackedUserId = dto.getBlackedUserId();
        // 查询是否已拉黑该用户
        ChatBlack exist = baseMapper.selectOne(new LambdaQueryWrapper<ChatBlack>()
                .eq(ChatBlack::getUserId, userId)
                .eq(ChatBlack::getBlackedUserId, blackedUserId));

        if (exist == null) {
            // 不存在拉黑记录，可以抛异常或直接返回
            throw new CommonException(ResultEnum.USER_IS_NOT_BLACKED.getMsg());
        }
        baseMapper.deleteById(exist);
    }
}
